<?php

class Register_email extends Controller 
{
	var $uid;
	var $tpl;

	function setVars()
	{
		$this->tpl['title'] = "Registration";
		$this->tpl['head'] = '';
	}
	
	function __construct()
	{
		parent::Controller();
		$this->setVars();
		log_message('debug', "Contact->Registration->email: Controler Class Initialized");
        $this->output->enable_profiler(TRUE);
	}

/*****************************************************************\
|               Register User Name
\*****************************************************************/	

	function index() 
	{
		$this->tpl['countryOptions'] = $this->country_options();
		if(isset($_POST['Alias']))
		{
			$this->postCheck();
		}
		else
		{
			$this->load->view('contact/register_email', $this->tpl);
		}
	}

	function postCheck()
	{
		log_message('debug', "Register email: Post check");
		$this->load->library('validation');
		$rules['Alias']			= "trim|required|min_length[5]|max_length[20]|xss_clean";
		$rules['eMail']			= "trim|required|valid_email|xss_clean";
		$rules['Country']		= "trim|required|xss_clean";
		$fields['Country']    	= 'Country';
		$this->validation->set_rules($rules);
		$this->validation->set_fields($fields);
		log_message('debug', "Register: Validation Rules set");
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('contact/register_email', $this->tpl);
		}
		else
		{
            $email = $this->validation->eMail;
            $alias = $this->validation->Alias;
            $sql = 'SELECT alias'
                    . ' FROM contact_people'
                    . ' JOIN (contact_peopleorguser, contact_user) '
                    . ' ON (contact_people.id = contact_peopleorguser.people_id)'
                    . ' AND ( contact_user.id = contact_peopleorguser.user_id)'
                    . " WHERE email = '$email' "
                    . ' AND user_role = 1;';
            $query = $this->db->query($sql);
			if ($query->num_rows() > 0)
			{
                $user_row = $query->row();
                if (strcasecmp($alias, $user_row->alias) == 0) {
                    $alias_match = "And your Alias and eMail match"; 
                }else{
                    $alias_match = "But your Alias and eMail don't match";
                }
                $this->msg->setError('You already have an account with us');
                $this->msg->setMsg($alias_match);
                $this->msg->setMsg('Try to login or');
                $this->msg->setMsg("use our don't remember link at the login page.");
                $this->load->view('contact/register_email', $this->tpl);
            }
            else
            {
            
            $this->user_create();
            #TODO: $this->Account->SendPass($query->alias, $email, $this->activation_key);
            redirect('account/secquestion', 'location');
            }
		}
	}
	
    
    function user_create()
    {
        $this->load->helper('string');
        $CTZ = $this->session->userdata('CTZ');
        $this->Country = $CTZ[$this->validation->Country]['FIPS'];
        $TZ = $CTZ[$this->validation->Country]['TZ_Linux'];
        $this->load->helper('string');
        $this->activation_key = random_string('unique');
        $user_data = array(
            'alias' => $this->validation->Alias,
            'email' 	=> $this->validation->eMail,
            'timezone' => $TZ,
            'country_fips'=> $this->Country,
            'IP' => $this->fun->getip(),
            'activation_key' => $this->activation_key,
            'added' => date("Y-m-d H:i:s") # We are in GMT timezone no need for gmdate
            );
        $this->db->insert('contact_register', $user_data); 
        return $user_data;
    }
    
    
    function country_options()
	{
		$output = '<select name="Country">';
		$output .= "<option value=\"\">--Location by Timezone--</option>\n";
		$tz = $this->session->userdata('TZ');
		if(isset($tz))
		{
			$sql = "
			SELECT places_timezone.id, FIPS, TZ_Linux, Country 
			FROM places_timezone
			JOIN (places_country) 
			ON (places_timezone.ISO3166 = places_country.ISO3166)
			WHERE TZ = '$tz'
			";
		}
		else
		{
			$sql = "
			SELECT places_timezone.id, FIPS, TZ_Linux, Country 
			FROM places_timezone
			JOIN (places_country) 
			ON (places_timezone.ISO3166 = places_country.ISO3166)
			";
		}
		
		$query = $this->db->query($sql);
		foreach ($query->result() as $row)
		{
			if(isset($tz))
			{
				$this->TZ_Linux = $row->TZ_Linux;
				$d['CTZ'][$row->id]['TZ_Linux'] = $row->TZ_Linux;
				$d['CTZ'][$row->id]['FIPS'] = $row->FIPS;
				$this->session->set_userdata($d);
				$l = strstr($row->TZ_Linux, '/');
				$c = $row->Country;
				$c = $c.$l;
			}
			$output .= "<option value=\"$row->id\"";
			$output .= ">$c</option>\n";
		}
		$output .= '</select>'; 
		return $output;		
	}
}
?>
